package com.atguigu.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.gulimall.auth.utils.HttpUtils;
import com.atguigu.gulimall.auth.vo.WeiboUserVo;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/oauth2")
public class Oauth2Controller {

    @RequestMapping("/weibo/redirec")
    public void weiboThirdLogin(@RequestParam String code) throws Exception{
//        https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
        Map<String,String> map = new HashMap<>();
        map.put("client_id", "2953322314");
        map.put("client_secret", "a7a4a723457e2461793ca73185a6c84e");
        map.put("grant_type", "authorization_code");
        map.put("code", code);
        map.put("redirect_uri", "http://auth.gulimall.com/oauth2/weibo/redirect");
        HttpResponse resp = HttpUtils.doGet("https://api.weibo.com", "/oauth2/access_token", "post", new HashMap<>(), map);
        String str = EntityUtils.toString((HttpEntity) resp);
        WeiboUserVo weiboUserVo = JSON.parseObject(str, WeiboUserVo.class);
        System.out.println(code);

    }
}
